/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.payment.provider.cybersource;

import net.martinimix.domain.payment.CreditCardAuthorization;

/**
 * Provides a basic credit card authorization decision manager implementation.
 * This class accepts authorizations if the authorization decision is in a
 * given list of accepted decisions.  By default, only the CybserSource decision
 * <code>ACCEPT</code> is treated as an accepted authorization.  This behavior
 * can be modified by setting a list of decisions that should be accepted using
 * {@link #setAcceptDecisions(String[])}. 
 *  
 * @author Scott Rossillo
 *
 */
public class SimpleAuthorizationDecisionManager implements
		AuthorizationDecisionManager {

	private String[] acceptDecisions;
	
	/**
	 * Creates a new simple credit card authorization decision manager.
	 */
	public SimpleAuthorizationDecisionManager() {
		acceptDecisions = new String[1];
		acceptDecisions[0] = "ACCEPT";
	}

	/**
	 * Returns true if the decision for this given credit card authorization should be accepted.
	 * By default, this method delegates the decision to {@link #acceptAuthorizationDecision(String)}.
	 * 
	 * @see net.martinimix.service.payment.provider.cybersource.AuthorizationDecisionManager#acceptAuthorization(net.martinimix.domain.payment.CreditCardAuthorization)
	 * @see #acceptAuthorizationDecision(String)
	 */
	public boolean acceptAuthorization(CreditCardAuthorization authorization) {
		
		return acceptAuthorizationDecision(authorization.getDecision());
	}
	
	/**
	 * Returns true if the given decision is equal to any of
	 * the values specified in this decision manager's assigned {@link #getAcceptDecisions()}.  
	 * By default, only a status equal to <code>ACCEPT</code> is accepted.
	 * 
	 * @see #setAcceptDecisions(String[])
	 */
	public final boolean acceptAuthorizationDecision(String decision) {
		
		for(int i = 0; i < acceptDecisions.length; i++) {
			if(acceptDecisions[i].equalsIgnoreCase(decision)) {
				return true;
			}
		}
		
		return false;
	}

	/**
	 * Returns the array of decisions this authorization decision manager will accept
	 * as authorized.
	 *  
	 * @return a <code>String</code> array containing the decisions this decision
	 * manager will accept as authorized
	 */
	public String[] getAcceptDecisions() {
		return acceptDecisions;
	}

	/**
	 * Sets the CyberSource authorization decision codes for which authorization
	 * should be accepted.
	 *  
	 * @param acceptDecisions an array of acceptable CyberSource decisions; may
	 * not be <code>null</code> or empty
	 */
	public void setAcceptDecisions(String[] acceptDecisions) {
		
		if(acceptDecisions == null || acceptDecisions.length == 0) {
			throw new IllegalArgumentException("[acceptDecisions] missing or empty!");
		}
		
		this.acceptDecisions = acceptDecisions;
	}

}
